import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: '首页',
        meta: { title: '首页', icon: 'el-icon-s-home', affix: true }
      }
    ]
  },
  {
    path: '/a-content/order',
    component: Layout,
    children: [
      {
        path: 'order-list-page',
        component: () => import('@/views/a-content/order/order-list-page'),
        name: '订单',
        meta: { title: '订单', icon: 'el-icon-s-order', noCache: true }
      }
    ]
  },
  {
    path: '/a-content/task/todo-list',
    component: Layout,
    children: [
      {
        path: 'todo-list',
        component: () => import('@/views/a-content/task/todo-list'),
        name: '我的待办',
        meta: { title: '我的待办', icon: 'el-icon-s-release', noCache: true }
      }
    ]
  },
  {
    path: '/a-content/task/done-list',
    component: Layout,
    children: [
      {
        path: 'done-list',
        component: () => import('@/views/a-content/task/done-list'),
        name: '已办事项',
        meta: { title: '已办事项', icon: 'el-icon-s-claim', noCache: true }
      }
    ]
  },
  {
    path: '/a-content/task/message-list',
    component: Layout,
    children: [
      {
        path: 'message-list',
        component: () => import('@/views/a-content/task/message-list'),
        name: '我的消息',
        meta: { title: '我的消息', icon: 'el-icon-s-comment', noCache: true }
      }
    ]
  },
  {
    path: '/a-content/customer-reference',
    component: Layout,
    redirect: '/a-content/customer-reference/add-bussiness',
    alwaysShow: true, // will always show the root menu
    name: '设置',
    meta: {
      title: '设置',
      icon: 'el-icon-s-tools',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'user-manage',
        component: () => import('@/views/a-content/setting/user-manage'),
        name: '用户管理',
        meta: {
          title: '用户管理',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'oauth-manage',
        component: () => import('@/views/a-content/setting/oauth-manage'),
        name: '权限管理',
        meta: {
          title: '权限管理',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'attach-type-manage',
        component: () => import('@/views/a-content/setting/attach-type-manage'),
        name: '附件类型管理',
        meta: {
          title: '附件类型管理',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'resource-manage',
        component: () => import('@/views/a-content/setting/resource-manage'),
        name: '资源管理',
        meta: {
          title: '资源管理',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'bussiness-manage',
        component: () => import('@/views/a-content/setting/bussiness-manage'),
        name: '商户管理',
        meta: {
          title: '商户管理',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'dealer-setting',
        component: () => import('@/views/a-content/setting/dealer-setting'),
        name: '经销商设置',
        meta: {
          title: '经销商设置',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'bank-setting',
        component: () => import('@/views/a-content/setting/bank-setting'),
        name: '银行设置',
        meta: {
          title: '银行设置'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'bank-prod-setting',
        component: () => import('@/views/a-content/setting/bank-prod-setting'),
        name: '银行产品设置',
        meta: {
          title: '银行产品设置'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        path: 'sign-setting',
        component: () => import('@/views/a-content/setting/sign-setting'),
        name: '签约设置',
        meta: {
          title: '签约设置',
          roles: ['admin']
        }
      },
      {
        path: 'pay-setting',
        component: () => import('@/views/a-content/setting/pay-setting'),
        name: '应付设置',
        meta: {
          title: '应付设置',
          roles: ['admin']
        }
      },
      {
        path: 'received-setting',
        component: () => import('@/views/a-content/setting/received-setting'),
        name: '应收设置',
        meta: {
          title: '应收设置',
          roles: ['admin']
        }
      },
      {
        path: 'org-setting',
        component: () => import('@/views/a-content/setting/org-setting'),
        name: '合作机构设置',
        meta: {
          title: '合作机构设置',
          roles: ['admin']
        }
      },
      {
        path: 'reg-addr-setting',
        component: () => import('@/views/a-content/setting/reg-addr-setting'),
        name: '上牌地设置',
        meta: {
          title: '上牌地设置',
          roles: ['admin']
        }
      },
      {
        path: 'fund-name-setting',
        component: () => import('@/views/a-content/setting/fund-name-setting'),
        name: '款项名称设置',
        meta: {
          title: '款项名称设置',
          roles: ['admin']
        }
      },
      {
        path: 'bank-account-setting',
        component: () => import('@/views/a-content/setting/bank-account-setting'),
        name: '银行账号设置',
        meta: {
          title: '银行账号设置',
          roles: ['admin']
        }
      }, {
        path: 'approve-setting',
        component: () => import('@/views/a-content/setting/approve-setting'),
        name: '审批流程设置',
        meta: {
          title: '审批流程设置',
          roles: ['admin']
        }
      },
      {
        path: 'prescription-setting',
        component: () => import('@/views/a-content/setting/prescription-setting'),
        name: '时效设置',
        meta: {
          title: '时效设置',
          roles: ['admin']
        }
      },
      {
        path: 'loan-public-setting',
        component: () => import('@/views/a-content/setting/loan-public-setting'),
        name: '贷款公示设置',
        meta: {
          title: '贷款公示设置',
          roles: ['admin']
        }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/a-content/customer-reference',
    component: Layout,
    name: '客户征信',
    meta: {
      title: '客户征信',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'add-bussiness',
        component: () => import('@/views/a-content/customer-reference/add-bussiness'),
        name: '业务新增',
        meta: {
          title: '业务新增',
          roles: ['admin'],
          noCache: true
        }
      },
      {
        path: 'bank-reference',
        component: () => import('@/views/a-content/customer-reference/bank-reference'),
        name: '银行征信',
        meta: {
          title: '银行征信',
          roles: ['admin']
        }
      },
      {
        path: 'bank-refer-detail',
        component: () => import('@/views/a-content/customer-reference/bank-refer-detail'),
        name: '银行征信详情',
        hidden: true,
        meta: {
          title: '银行征信详情',
          roles: ['admin']
        }
      },
      {
        path: 'big-data',
        component: () => import('@/views/a-content/customer-reference/big-data'),
        name: '大数据',
        meta: {
          title: '大数据',
          roles: ['admin']
        }
      }
    ]
  },
  {
    path: '/a-content/loan/loan-apply-list',
    component: Layout,
    children: [
      {
        path: 'loan-apply-list',
        component: () => import('@/views/a-content/loan/loan-apply-list'),
        name: '贷款申请单',
        meta: { title: '贷款申请单', icon: 'icon', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/loan',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'reference-detail-main',
        component: () => import('@/views/a-content/loan/reference-detail-main'),
        name: '申请单明细',
        meta: { title: '申请单明细', icon: 'icon', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/risk-control/',
    component: Layout,
    children: [
      {
        path: 'risk-control-audit',
        component: () => import('@/views/a-content/risk-control/risk-control-audit'),
        name: '风控审核',
        meta: { title: '风控审核', icon: 'icon', noCache: true, roles: ['admin'] }
      } ,
      {
        path: 'risk-control-detail-form',
        hidden: true,
        component: () => import('@/views/a-content/risk-control/risk-control-detail-form'),
        name: '风控审核详情',
        meta: { title: '风控审核', icon: 'icon', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/manager/sign-manager',
    component: Layout,
    children: [
      {
        path: 'sign-manager',
        component: () => import('@/views/a-content/manager/sign-manager'),
        name: '签约管理',
        meta: { title: '签约管理', icon: 'icon', noCache: true, roles: ['admin'] }
      },
      {
        path: 'sign-detail',
        component: () => import('@/views/a-content/manager/sign-detail'),
        name: '签约详情',
        hidden: true,
        meta: { title: '签约详情', icon: 'icon', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/manager/bank-loan-manager',
    component: Layout,
    children: [
      {
        path: 'bank-loan-manager',
        component: () => import('@/views/a-content/manager/bank-loan-manager'),
        name: '银行放款',
        meta: { title: '银行放款', icon: 'icon', noCache: true , roles: ['admin']}
      },
      {
        path: 'bank-loan-detail',
        component: () => import('@/views/a-content/manager/bank-loan-detail'),
        name: '银行放款详情',
        hidden: true,
        meta: { title: '银行放款详情', icon: 'icon', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/manager/mortgage-manager',
    component: Layout,
    children: [
      {
        path: 'mortgage-manager',
        component: () => import('@/views/a-content/manager/mortgage-manager'),
        name: '抵押管理',
        meta: { title: '抵押管理', icon: 'icon', noCache: true, roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/manager/file-manager',
    component: Layout,
    children: [
      {
        path: 'file-manager',
        component: () => import('@/views/a-content/manager/file-manager'),
        name: '归档',
        meta: { title: '归档', icon: 'icon', noCache: true , roles: ['admin']}
      },
      {
        path: 'file-detail',
        component: () => import('@/views/a-content/manager/file-detail'),
        name: '归档详情',
        hidden: true,
        meta: { title: '归档详情', icon: 'icon', noCache: true,  roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/manager/settle-manager',
    component: Layout,
    children: [
      {
        path: 'settle-manager',
        component: () => import('@/views/a-content/manager/settle-manager'),
        name: '结清',
        meta: { title: '结清', icon: 'icon', noCache: true, roles: ['admin'] }
      },
      {
        path: 'settle-detail',
        component: () => import('@/views/a-content/manager/settle-detail'),
        name: 'settleDetail',
        hidden: true,
        meta: { title: '结清详情', icon: 'icon', noCache: true ,roles: ['admin']  }
      }
    ]
  },
  {
    path: '/a-content/gps-center',
    component: Layout,
    name: 'GPS中心',
    meta: {
      title: 'GPS中心',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'gps-not-installed',
        component: () => import('@/views/a-content/gps-center/gps-not-installed'),
        name: 'GPS未安装',
        meta: {
          title: 'GPS未安装',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'gps-installed',
        component: () => import('@/views/a-content/gps-center/gps-installed'),
        name: 'GPS已安装',
        meta: {
          title: 'GPS已安装',
          roles: ['admin']
          // if do not set roles, means: this page does not require permission
        }
      }
    ]
  }, {
    path: '/a-content/manager/overdue-manager',
    component: Layout,
    children: [
      {
        path: 'overdue-manager',
        component: () => import('@/views/a-content/manager/overdue-manager'),
        name: '超期管理',
        meta: { title: '超期管理', icon: 'icon', noCache: true ,roles: ['admin'] }
      }
    ]
  },
  {
    path: '/a-content/finance',
    component: Layout,
    name: '财务中心',
    meta: {
      title: '财务中心',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'receivable-list',
        component: () => import('@/views/a-content/finance/receivable-list'),
        name: '应收列表',
        meta: {
          title: '应收列表',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'payable-list',
        component: () => import('@/views/a-content/finance/payable-list'),
        name: '应付列表',
        meta: {
          title: '应付列表',
          roles: ['admin']
          // if do not set roles, means: this page does not require permission
        }
      }
    ]
  },
  /** when your routing map is too long, you can split it into small modules **/
  // componentsRouter,
  // chartsRouter,
  // tableRouter,

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes.concat(asyncRoutes)
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter(params) {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

router.selfaddRoutes = function (params){
  router.matcher = new Router().matcher;
  router.addRoutes(params)
}
export default router
